Repetition in functional languages

Metadata
aliases: []
shorthands: {}
created: 2022-07-24 22:09:19
modified: 2022-07-24 22:18:19

In functional languages, new values are associated with new names through recursive functions call nesting.

Example

To sum an array, we can use the following function:

FUNCTION SUM(A:ARRRAY [1..N] OF INTEGER; I,N:INTEGER):INTEGER;
BEGIN
    IF I > N THEN
        SUM := 0
    ELSE
        A[I] + SUM(A,I+1,N)
END

Here, each recursive call to SUM creates new local versions of A, I and N and the previous versions become inaccessible.